package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import com.google.gson.Gson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class AddClueActivityRepository {
    fun editClue(editClueParam: EditClueParam, moth: (clueDetails: ClueDetails) -> Unit,fail:()->Unit,withEffect:Boolean = true) {
        var json = Gson().toJson(editClueParam)
        var map = Gson().fromJson(json, HashMap<String, String>()::class.java)
        if(!withEffect)
            map.remove("effective")
        ApiClient.getInstance().editClue(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueDetails>>() {
                    override fun onSuccess(t: BaseResp<ClueDetails>) {
                        t.data?.apply(moth)
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun editClue2(editClueParam: EditClueParam, moth: () -> Unit,fail:()->Unit,withEffect:Boolean = true) {
        var json = Gson().toJson(editClueParam)
        var map = Gson().fromJson(json, HashMap<String, String>()::class.java)
        if(!withEffect)
            map.remove("effective")
        ApiClient.getInstance().editClue(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueDetails>>() {
                    override fun onSuccess(t: BaseResp<ClueDetails>) {
                        moth()
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    /**
     * 检查电话号码是否存在
     */
    fun IsPhone(phone: String, kFunction1: (item: IsPhoneParam) -> Unit) {
        ApiClient.getInstance().isPhone(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<IsPhoneParam>>() {
                    override fun onSuccess(t: BaseResp<IsPhoneParam>) {
                        t.data?.apply {
                            kFunction1(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun getlevel(followstate:String,tid:String, moth: (levels: ClueLevel) -> Unit,fail:()->Unit) {
        ApiClient.getInstance().getClueLevel(followstate,tid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueLevel>>() {
                    override fun onSuccess(t: BaseResp<ClueLevel>) {
                        t.data?.apply(moth)
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun getFollowStatus(effective:String, tid:String, moth: (data: Followstate) -> Unit, fail:()->Unit) {
        ApiClient.getInstance().getFollowStatus(effective,tid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Followstate>>() {
                    override fun onSuccess(t: BaseResp<Followstate>) {
                        t.data?.apply(moth)
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }
}